package com.wandian.cqcsk.mvvm.mine.mywallet.mycoinhistory;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.adapter.mine.MyCoinHistoryRecycleAdapter;
import com.wandian.cqcsk.adapter.mine.MyTXHistoryRecycleAdapter;
import com.wandian.cqcsk.databinding.ActivityMyCoinHistoryBinding;
import com.wandian.cqcsk.databinding.ActivityMyTxHistoryBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 金币记录
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_COIN_HISTORY)
public class MyCoinHistoryActivity extends BaseMVVMActivity<MyCoinHistoryViewModel> implements BaseQuickAdapter.RequestLoadMoreListener {
    private ActivityMyCoinHistoryBinding dataBinding;
    private MyTXHistoryRecycleAdapter adapter;
    private int pageNum = 1;
    private int pageSize = 30;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_my_coin_history);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initRecycle();
        initClink();
        mViewModel.getDataList(pageNum,pageSize,adapter);
    }
    private void initViewData(){
        //设置状态颜色和字体黑色
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.sys_color))
                .init();
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("金币记录");
        mViewModel.listData.observe(this,listData->{
            if(!UIUtils.isEmpty(listData) && listData.size()>0){
                if(pageNum==1){
                    adapter.setNewData(listData);
                }else {
                    adapter.addData(listData);
                    adapter.loadMoreComplete();
                }
            }else {
                adapter.setEnableLoadMore(false);
            }
        });

    }
    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finishhideSoftBoard();
            finish();
        });
    }
    private void  initRecycle(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dataBinding.recycle.setLayoutManager(layoutManager);
        adapter = new MyTXHistoryRecycleAdapter(R.layout.item_mine_coin_history_style);
        dataBinding.recycle.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this, dataBinding.recycle);
        adapter.setEnableLoadMore(false);
    }


    @Override
    public void onLoadMoreRequested() {
        if(!UIUtils.isEmpty(adapter) && adapter.getData().size()>=pageSize){
            pageNum = pageNum+1;
            mViewModel.getDataList(pageNum,pageSize,adapter);
        }else {
            adapter.setEnableLoadMore(false);
        }
    }
}
